<template>
  <div>
    <div>
      <el-form style="text-align: left" :model="searchMsg" status-icon :inline="true" ref="searchFrom"
               label-width="100px" class="demo-form-inline">
        <el-form-item label="昵称" prop="nikename">
          <el-input v-model="searchMsg.nikename"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="searchMsg.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="searchMsg.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="custname">
          <el-input v-model="searchMsg.custname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio v-model="searchMsg.sex" label="1">男</el-radio>
          <el-radio v-model="searchMsg.sex" label="2">女</el-radio>
        </el-form-item>
        <br>
        <el-form-item style="margin-left:  500px">
          <el-button type="primary" @click="search()">搜索</el-button>
          <el-button @click="restSearch()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="text-align: left">
      <el-button type="danger" @click="removeSelect()">删除选中</el-button>
    </div>
    <el-table
        ref="multipleTable"
        :data="customerMsgs"
        tooltip-effect="dark"
        style="width: 100%"
        border
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          label="头像"
          width="120">
        <template slot-scope="scope">
          <div class="demo-basic--circle">
            <div class="block" style="padding-top: 20px;padding-bottom: 20px">
              <el-avatar :size="50" :src="scope.row.headurl"></el-avatar>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
          prop="custname"
          label="姓名"
      >
      </el-table-column>
      <el-table-column
          prop="nikename"
          label="昵称"
      >
      </el-table-column>
      <el-table-column
          prop="address"
          label="地址"
      >
      </el-table-column>
      <el-table-column
          prop="phone"
          label="注册电话"
      >
      </el-table-column>
      <el-table-column
          label="性别"
      >
        <template slot-scope="scope">
          {{ scope.row.sex === 1 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="350px"
      >
        <template slot-scope="scope">
          <el-button @click="popModifyMenu(scope.row.cid)" type="primary" size="small">编辑</el-button>
          <el-button @click="restPwd(scope.row.cid)" type="warning" size="small">重置密码</el-button>
          <el-button @click="removeOne(scope.row.cid)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[3,5,10,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <!--    修改-->
    <el-dialog
        title="修改"
        :visible.sync="modifyFlag"
        width="30%"
        :before-close="handleClose">
      <el-form :model="customerMsg" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户头像" prop="headurl">
          <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:8081/Util/FileUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar" width="260px" height="260px">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nikename">
          <el-input v-model="customerMsg.nikename"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="custname">
          <el-input v-model="customerMsg.custname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="customerMsg.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio v-model="customerMsg.sex" label="1">男</el-radio>
          <el-radio v-model="customerMsg.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="modifyUser('userMsg')">提交</el-button>
          <el-button @click="resetForm('userMsg')">重置</el-button>
          <el-button @click="modifyFlag = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "usermenu",
  data() {
    return {
      // 用户数据
      customerMsgs: [],
      // 选择框数据
      multipleSelection: [],
      // 分页数据
      pageIndex: 1,
      pageSize: 5,
      total: 0,
      // 修改的标记
      modifyFlag: false,
      // 分配角色的标记
      roleFlag: false,
      // 要被添加的用户信息
      customerMsg: {
        loginname: '',
        pwd: '',
        realname: '',
        address: '',
        sex: 0,
      },
      // 临时保存的用户id
      tempUserId: 0,
      // 搜索的条件
      searchMsg: {
        nikename:"",
        phone:"",
        address:"",
        custname:"",
        sex:-1
      },
      // 角色的全部信息
      roleMsgs: [],
      // 头像图片的url信息
      imageUrl:"",
    };
  },
  // 页面初始化方法
  created() {
    this.flushPage()
  },
  methods: {
    // 复选框的方法
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 当角色选取框发生改变的时候
    roleSelectionChange(val) {
      this.roleMultipleSelection = val;
    },
    // 分页的方法，当页面大小改变
    handleSizeChange(val) {
      this.pageSize = val
      this.flushPage()
    },
    // 当页面下标改变
    handleCurrentChange(val) {
      this.pageIndex = val
      this.flushPage()
    },
    // 删除这条记录
    removeOne(userid) {
      let ids = []
      ids.push(userid)
      this.removeBeach(ids)
    },
    // 删除选中
    removeSelect() {
      let ids = []
      this.multipleSelection.forEach(row => {
        ids.push(row.cid)
      })
      this.removeBeach(ids)
    },
    // 批量删除记录
    removeBeach(ids) {
      const h = this.$createElement
      // 发送请求
      if (ids.length<=0){
        this.$notify.error({
          title: '错误',
          message: '请至少选中一个用户删除！'
        });
        return;
      }
      this.$http.post("/Customer/Remove", ids).then(resp => {
        if (resp.data.code === 2000) {
          // 消息通知
          this.$notify({
            title: "消息",
            message: h("i", {style: "color: teal"}, resp.data.msg),
            duration: 4000
          })
          // 刷新页面
          this.flushPage()
        }
      })
    },
    // 弹出修改
    popModifyMenu(id) {
      this.$http.get("Customer/GetMsg/" + id).then(resp => {
        this.customerMsg = resp.data.data
        this.customerMsg.pwd=""
        this.imageUrl=resp.data.data.headurl
      })
      this.modifyFlag = true
    },
    // 重置该用户的密码
    restPwd(uid) {
      this.$http.get("Customer/RestPwd/" + uid).then(resp => {
        if (resp.data.code === 2000) {
          this.$notify({
            title: '成功',
            message: '用户的密码重置成功！默认为123456',
            type: 'success'
          });
        } else {
          this.$notify.error({
            title: '错误',
            message: '密码重置失败'
          });
        }
      })
    },
    // 修改用户信息
    modifyUser() {
      this.customerMsg.headurl=this.imageUrl
      this.$http.post("/Customer/Modify", this.customerMsg).then(resp => {
        console.log(this.customerMsg)
        if (resp.data.code === 2000) {
          this.$notify({
            title: "消息",
            message: "更新成功",
            duration: 4000
          })
        } else {
          this.$notify.error({
            title: '错误',
            message: '更新失败'
          });
        }
      })
      this.handleClose()
      this.flushPage()
    },
    // 对话框关闭之后
    handleClose() {
      this.$nextTick(() => {
        this.$refs['ruleForm'].resetFields();
      })
      this.addFlag = false
      this.modifyFlag = false
    },
    resetForm() {
      this.$nextTick(() => {
        this.$refs['ruleForm'].resetFields();
      })
    },
    // 重置搜索菜单
    restSearch() {
      this.$nextTick(() => {
        this.$refs['searchFrom'].resetFields();
      })
    },
    // 执行搜索方法
    search() {
      this.$http.post("/Customer/GetCustomers", this.searchMsg).then(resp => {
        this.customerMsgs = resp.data.data
      })
    },
    // 刷新页面的方法
    flushPage() {
      // 获取全部的用户信息
      this.$http.get("/Customer/List/" + this.pageIndex + "/" + this.pageSize).then(resp => {
        console.log(resp.data.data)
        // 放入用户信息
        this.customerMsgs = resp.data.data.records;
        // 放入分页数据
        this.total = resp.data.data.total
      })
    },
    // 图片上传工具方法
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
  }
}

</script>

<style scoped>

</style>
